import type { App } from "vue";
import { createRouter, createWebHistory, type RouteRecordRaw } from "vue-router";

export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },
  {
    path: "/applicationCenter",
    component: () => import("@/views/login/applicationCenter.vue"),
    meta: { hidden: true },
  },
  {
    path: "/modifyPassword",
    component: () => import("@/views/login/modifyPassword.vue"),
    meta: { hidden: true },
  },
  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "dashboard",
        meta: {
          title: "dashboard",
          icon: "el-icon-reading",
          affix: true,
          keepAlive: true,
        },
      },
      {
        path: "401",
        component: () => import("@/views/error/401.vue"),
        meta: { hidden: true },
      },
      {
        path: "404",
        component: () => import("@/views/error/404.vue"),
        meta: { hidden: true },
      },
    ],
  },
];

// 动态路由
export const asyncRouterMap: RouteRecordRaw[] = [
  {
    path: "/permissionsManage",
    component: Layout,
    redirect: "/permissionsManage/messageSubscribe",
    name: "/permissionsManage",
    meta: {
      title: "permissionsManage",
      icon: "el-icon-setUp",
      hidden: false,
      keepAlive: true,
      alwaysShow: true,
    },
    children: [
      {
        path: "messageSubscribe",
        component: () => import("@/views/permissionsManage/messageSubscribe/index.vue"),
        name: "messageSubscribe",
        meta: {
          title: "messageSubscribe",
          icon: "el-icon-pointer",
          hidden: false,
          keepAlive: true,
          alwaysShow: false,
        },
      },
    ],
  },
  {
    path: "/dataManage",
    component: Layout,
    redirect: "/dataManage/APICallStatistics",
    name: "/dataManage",
    meta: {
      title: "dataManage",
      icon: "el-icon-files",
      hidden: false,
      keepAlive: true,
      alwaysShow: true,
    },
    children: [
      {
        path: "APICallStatistics",
        component: () => import("@/views/dataManage/APICallStatistics/index.vue"),
        name: "APICallStatistics",
        meta: {
          title: "APICallStatistics",
          icon: "el-icon-operation",
          hidden: false,
          keepAlive: true,
          alwaysShow: false,
        },
      },
      {
        path: "messagePushStatistics",
        component: () => import("@/views/dataManage/messagePushStatistics/index.vue"),
        name: "messagePushStatistics",
        meta: {
          title: "messagePushStatistics",
          icon: "el-icon-document",
          hidden: false,
          keepAlive: true,
          alwaysShow: false,
        },
      },
    ],
  },
  {
    path: "/memberManage",
    component: Layout,
    redirect: "/memberManage/memberList",
    name: "/memberManage",
    meta: {
      title: "memberManage",
      icon: "el-icon-User",
      hidden: false,
      keepAlive: true,
      alwaysShow: false,
    },
    children: [
      {
        path: "memberList",
        component: () => import("@/views/memberManage/memberList/index.vue"),
        name: "memberList",
        meta: {
          title: "memberList",
          icon: "el-icon-User",
          hidden: false,
          keepAlive: true,
          alwaysShow: false,
        },
      },
    ],
  },
];
/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHistory("/openPlatform"),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router);
}

export default router;
